<?php
namespace Admin\Controller;
use Think\Controller;

    class AdminLoginController extends Controller
    {
        public function login()
        {
          // 接受用户名和密码
          $adminuser = I('post.name');
          $password=I('post.pwd');
          // 验证
          $admin = M('sh_admin_user');
          $data =  $admin->where(array('name'=>$adminuser))->find();
           // if ($data != null) {
           //        session('AdminName',$_POST['name']);
           //        $this->assign('AdminName',session('AdminName'));
           //        $this->display('Index/index');
           // }else{
           //        $this->display('AdminLogin/login');
           // }
           
           if(!$data){
                  $this->error('用户名不存在!');
                  exit;
           }

           if($data['pwd'] !=md5($password)){
                  $this ->error('密码不正确!');
                  exit;
           }
           // 把用户信息填写到session中
           $_SESSION['admin_user']=$data;

           // dump($data['id']);
           // dump($_SESSION);exit;
           // 根据用户ID获取节点信息
            $list = M('sh_node')->field('mname,aname')->where('id in'.M('sh_role_node')->field('nid')->where("rid in ".M('sh_admin_role')->field('rid')->where(array('uid'=>array('eq',$data['id'])))->buildSql())->buildSql())->select();

            //控制器名转换为大写
            foreach ($list as $key => $val){
              $list[$key]['mname'] = ucfirst($val['mname']);
            }
  
                    //查看查询出来的信息
            // dump($list); exit;
  
            $nodelist = array();
            $nodelist['Index'] = array('index');
            // dump($nodelist);exit;

            //遍历重新拼装
            foreach($list as $v){
              $nodelist[$v['mname']][] = $v['aname'];
              //把修改和执行修改 添加和执行添加 拼装到一起
              if($v['aname']=="edit"){
                $nodelist[$v['mname']][]="save";
              }
              if($v['aname']=="add"){
                $nodelist[$v['mname']][]="doadd";
              }
            }
            // dump($_SESSION);
            // dump('$nodelist') exit;
                    //将权限信息放置到session中
            $_SESSION['admin_user']['nodelist'] = $nodelist;
  
                    //重组的信息
            // dump($_SESSION);exit;
            
            //跳转到首页
            $this->redirect('Index/index');
        }

        // 退出登录
        public function loginout()
        {
            // 清空session
            unset($_SESSION['admin_user']);
            // 跳转
            $this->display('AdminLogin/login');

        }
    }